package com.boesch.electroOffice.presentation.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;

@FacesConverter("LocalDateTimeConverter")
public class LocalDateTimeConverter implements Converter {
	
	private static final String PATTERN = "dd.MM.yyyy hh:mm";

	public Object getAsObject(FacesContext context, UIComponent component, String value) {
        LocalDateTime dateTime = null;
        try{
            dateTime = DateTimeFormat.forPattern(PATTERN).parseLocalDateTime(value);
        }catch (Exception ex){
            throw new ConverterException(ex);
        }
        return dateTime;
	}

	public String getAsString(FacesContext context, UIComponent component, Object value) {
        String dateTime = null;
        try{
            dateTime = DateTimeFormat.forPattern(PATTERN).print((LocalDateTime) value);
        }catch (Exception ex){
            throw  new ConverterException(ex);
        }
        return dateTime;
	}
}
